package com.cdelink.xzgy.ftp.config;


import com.cdelink.xzgy.ftp.util.JwtUtil;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

import static com.cdelink.xzgy.ftp.config.AppConfig.HEADER;


@Configuration
@EnableSwagger2

public class SwaggerConfig {

    @Bean
    public Docket createDocket() {
        return createDocket("com.cdelink.xzgy.ftp.user.controller");
    }

    public Docket createDocket(String basePackage) {

        return new Docket(DocumentationType.SWAGGER_2).select()
                .apis(RequestHandlerSelectors.basePackage(basePackage))
                .build().enable(true)
                .securitySchemes(securitySchemes())
                .securityContexts(securityContexts())
                .apiInfo(apiInfo());
    }

    private List<ApiKey> securitySchemes() {
        List<ApiKey> apiKeyList = new ArrayList<>();
        apiKeyList.add(new ApiKey(JwtUtil.KEY_USER_TOKEN, JwtUtil.KEY_USER_TOKEN, HEADER));
        return apiKeyList;
    }


    /**
     * 在Swagger中配置开发者的联系方式（姓名，url，邮箱）
     *
     * @return 开发者联系信息Contact对象
     */
    protected Contact getDeveloper() {
        return null;
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //页面标题
                .title("xxx")
                .version("1.0")
                .contact(getDeveloper())
                //描述
                .description("API描述文档")
                .build();
    }
    private List<SecurityContext> securityContexts() {
        List<SecurityContext> securityContextList = new ArrayList<>();
        List<SecurityReference> securityReferenceList = new ArrayList<>();
        securityReferenceList.add(new SecurityReference("user_token", scopes()));
        securityContextList.add(SecurityContext
                .builder()
                .securityReferences(securityReferenceList)
                .forPaths(PathSelectors.any())
                .build()
        );
        return securityContextList;
    }

    private AuthorizationScope[] scopes() {
        return new AuthorizationScope[]{new AuthorizationScope("global", "accessAnything")};
    }


}